﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// PageUser.xaml 的交互逻辑
    /// </summary>
    public partial class PageUser : Page
    {
        public PageUser()
        {
            InitializeComponent();
        }
        UserBusiness userBusiness = new UserBusiness();

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //dgUser
            var list = userBusiness.GetAllUser();
            dgUser.ItemsSource = list;
        }

        //回车键提交，或者鼠标点击其他单元格
        private void dgUser_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            var user = e.Row.Item as Login_user;
            object selectedValue = null;
            //账号。
            if (e.Column.SortMemberPath.Equals("User_account") || e.Column.SortMemberPath.Equals("User_real_name"))
            {
                selectedValue = ((System.Windows.Controls.TextBox)e.EditingElement).Text;
            }
            //角色
            else if (e.Column.SortMemberPath.Equals("Role_id"))
            {
                selectedValue = ((System.Windows.Controls.Primitives.Selector)e.EditingElement).SelectedValue as string;
            }
            //备注
            else if (e.Column.SortMemberPath.Equals("Remark"))
            {
                selectedValue = ((System.Windows.Controls.TextBox)e.EditingElement).Text;
            }
            //状态
            else if (e.Column.SortMemberPath.Equals("U_stat"))
            {
                selectedValue = (bool)((System.Windows.Controls.Primitives.ToggleButton)e.EditingElement).IsChecked;
                if (user.Id == 0)
                {
                    e.Cancel = true;
                    return;
                }
            }
            if (user.Id == 0)
            {
                //新增一个人。//添加时间：2022-12-6 17:35:58
                Login_user userInfo = new Login_user();
                userInfo.User_account = selectedValue.ToString();
                var addResult = userBusiness.AddUser(userInfo);

                //刷新数据
                var list = userBusiness.GetAllUser();
                dgUser.ItemsSource = list;

                ShowAlertHelpter.ShowAlert(addResult.Msg);
            }
            else
            {
                //修改，保存
                var result = userBusiness.Update(e.Column.SortMemberPath, selectedValue, user.Id);
                ShowAlertHelpter.ShowAlert(result.Msg);
            }
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            var list = userBusiness.GetAllUser();
            var listResult = list.Where(g => g.User_account.Contains(txtAccount.Text.Trim())).ToList();

            //必须要List，否则不能编辑单元格
            dgUser.ItemsSource = listResult;
        }

        //上传签名
        private void BtnImgName_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.Filter = "image|*.png";
            if ((bool)ofd.ShowDialog())
            {
                //ShowAlertHelpter.ShowAlert(ofd.FileName);

                //保存图片
                string dir = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, CommonConfig.UploadImgSavePath);
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }
                string file = DateTime.Now.Ticks + ".png";
                string fileSavePath = System.IO.Path.Combine(dir, file);
                var buffer = System.IO.File.ReadAllBytes(ofd.FileName);
                System.IO.File.WriteAllBytes(fileSavePath, buffer);

                //当前人，修改签名
                string relativePath = System.IO.Path.Combine(CommonConfig.UploadImgSavePath, file);
                //Login_user login_User = this.dgUser.SelectedItem as Login_user;
                var button = sender as Button;
                if (button != null)
                {
                    long id = (long)button.Tag.ToMyInt64();
                    var result = userBusiness.Update("SignatureImgPath", relativePath, id);
                    if (result.Code == (int)ResultCode.Success)
                    {
                        btnSearch_Click(sender,e);//刷新列表数据
                        ShowAlertHelpter.ShowAlert("签名保存成功");
                    }
                    else
                    {
                        ShowAlertHelpter.ShowAlert(result.Msg);
                    }

                }

            }
        }

        //重置密码 //添加时间：2023-1-10 10:17:37。作者：王浩力
        private void ResetPassword_Click(object sender, RoutedEventArgs e)
        {
            WindowUserResetPassword windowUserReset = new WindowUserResetPassword();
            windowUserReset.selectedUser = this.dgUser.CurrentItem as Login_user;
            windowUserReset.ShowDialog();

        }
    }
}
